﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SZHL.UI.AppCode;
using System.Reflection;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class CCQJManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(CCQJManage).GetMethod(msg.Action.ToUpper());
            CCQJManage model = new CCQJManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加出差请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户信息</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDCCQJ(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_CCXJ ccxj = JsonConvert.DeserializeObject<ZH_CCXJ>(P1);

            if (ccxj.Daycount == 0)
            {
                msg.ErrorMsg = "申请天数不能为空";
                return;
            }
            if (ccxj.EndTime < ccxj.StarTime)
            {
                msg.ErrorMsg = "开始时间不能大于结束时间";
                return;
            }
            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(ccxj.Files))
                {
                    ccxj.Files += "," + fids;
                }
                else
                {
                    ccxj.Files = fids;
                }
            }
            if (ccxj.ID == 0)
            {
                ccxj.ShenQingRen = string.IsNullOrEmpty(ccxj.ShenQingRen) ? UserInfo.User.UserRealName : ccxj.ShenQingRen;
                ccxj.CRDate = DateTime.Now;
                ccxj.CRUser = UserInfo.User.UserName;
                ccxj.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                ccxj.BranchNo = UserInfo.User.BranchCode;
                new ZH_CCXJB().Insert(ccxj);


                string jsr = new Yan_WF_TIB().GetEntities(d => d.PIID.Value == ccxj.intProcessStanceid && d.EndTime == null).ToList()[0].TaskUserID;
                if (!string.IsNullOrEmpty(jsr))
                {
                    //发送消息
                    string content = UserInfo.User.UserRealName + "发起了一个请假出差单，请您查阅审核";
                    new JH_Auth_User_CenterB().SendMsg(UserInfo, "ccxj", "请假出差", content, ccxj.ID, jsr, ccxj.intProcessStanceid);
                }
            }
            else
            {
                new ZH_CCXJB().Update(ccxj);
            }
            msg.Result = ccxj;
        }

        /// <summary>
        /// 获取出差请假列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">请假内容</param>
        /// <param name="UserInfo"></param>
        public void GETCCQJLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " Where  1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And cc.LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  cc.ZhuYaoShiYou like '%{0}%'", P2);
            }
            string strSql = string.Format(@"select cc.ShenQingRen,cc.LeiBie,cc.StarTime,cc.EndTime,cc.Daycount,cc.ZhuYaoShiYou,cc.ID,cc.BranchName , 
                                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName,intProcessStanceid
                                            from ZH_CCXJ cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID " + strWhere + " order by cc.CRDate desc");
            msg.Result = new ZH_CCXJB().GetDTByCommand(strSql);

          
        }
        /// <summary>
        /// 获取出差请假分页列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">请假内容</param>
        /// <param name="UserInfo"></param>
        public void GETCCQJLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "  1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  ZhuYaoShiYou like '%{0}%'", P2);
            }
            string b = context.Request.QueryString["b"] ?? "";
            if (b != "")
            {

                strWhere += string.Format(" And  branchNo= '{0}'", b);
            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            string colNme = @"cc.ShenQingRen,cc.LeiBie,cc.StarTime,cc.EndTime,cc.Daycount,cc.ZhuYaoShiYou,cc.ID,cc.BranchName , 
                                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName,intProcessStanceid";
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_CCXJ cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID", colNme, 8, page, " cc.CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;

        }
        /// <summary>
        /// 删除请假出差
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELCCQJBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_CCXJB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取出差请假信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETCCQJMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_CCXJ ccxj = new ZH_CCXJB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = ccxj;
            if (!string.IsNullOrEmpty(ccxj.Files))
            {
                msg.Result2 = new FT_FileB().GetEntities(d => ccxj.Files.Split(',').Contains(d.ID.ToString()));
            }
        }


        /// <summary>
        /// 我的请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETMYCCQJLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " Where  1=1 ";
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser != "")
                {
                    strWhere += string.Format(" And  cc.CRUser in('{0}')", strQueryUser.ToFormatLike());
                }
                else
                {
                    strWhere += string.Format("  And cc.BranchNo = '{0}'", UserInfo.User.BranchCode);
                }
            }
            else
            {
                strWhere += string.Format(" And cc.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format(" And cc.LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  cc.ZhuYaoShiYou like '%{0}%'", P2);
            }

            string strSQL = string.Format(@"SELECT cc.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                        when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName
                                                        from ZH_CCXJ cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID   " + strWhere + " order by cc.CRDate desc");
            msg.Result = new Yan_WF_TDB().GetDTByCommand(strSQL);

            
        }
        /// <summary>
        /// 获取工作日报菜单的日报数量
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETCCQJMENUCOUNT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = "select Count(0) from ZH_CCXJ";
            string strWhere = string.Format(" where  BranchNo= '{0}'", UserInfo.User.BranchCode);
            msg.Result = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);
            strWhere = string.Format(" where CRUser= '" + UserInfo.User.UserName + "'");
            msg.Result1 = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);

        }
        /// <summary>
        /// 我的请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETMYCCQJLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " 1=1";
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser == "")
                { 
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branch);
                }
                strWhere += string.Format(" And  cc.CRUser in('{0}')", strQueryUser.ToFormatLike());
            }
            else
            {
                strWhere += string.Format(" And cc.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format(" And cc.LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  cc.ZhuYaoShiYou like '%{0}%'", P2);
            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            string colNme = @"cc.ShenQingRen,cc.LeiBie,cc.StarTime,cc.EndTime,cc.Daycount,cc.ZhuYaoShiYou,cc.ID,cc.BranchName , 
                                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName,intProcessStanceid";
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_CCXJ cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID", colNme, 8, page, " cc.CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;
        }
    }

}